//
//  JMColorContainer.m
//  MasterBoard
//
//  Created by 赵俊明 on 2018/12/28.
//  Copyright © 2018 赵俊明. All rights reserved.
//

#import "JMColorContainer.h"
#import "JMColorView.h"
#import "JMRGBTextView.h"
#import "UIImage+JMImage.h"
#import "UIView+Extension.h"
#import "JMMacro.h"
#import "StaticClass.h"

@interface JMColorContainer()
@property (nonatomic, weak) JMColorView *colorView;
@property(nonatomic, weak) JMRGBTextView *RGBView;
@end

@implementation JMColorContainer

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = JMColor(100, 100, 100);
        self.layer.cornerRadius = 20;
        self.layer.masksToBounds = YES;
        
        JMSelf(ws);
        JMColorView *colorView = [[JMColorView alloc] initWithFrame:CGRectMake(15, 10, self.width-30, self.width-30)];
        [self addSubview:colorView];
        self.colorView = colorView;
        colorView.colorBlock = ^(UIColor *color) {
            
            [ws.RGBView set_RGBWithColor:color];
        };
        
        JMRGBTextView *RGBView = [[JMRGBTextView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(colorView.frame)+10, self.width-20, 120)];
        [self addSubview:RGBView];
        self.RGBView = RGBView;
        RGBView.sliderValue = ^(CGFloat value, BOOL isAlpha) {
            if (ws.showValuesBlock) {ws.showValuesBlock(value, isAlpha);}
        };
        
        UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        close.tintColor = [UIColor whiteColor];
        [close setImage:[UIImage imageWithTemplateName:@"navbar_close_icon_black"] forState:(UIControlStateNormal)];
        [close addTarget:self action:@selector(closeView:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:close];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _colorView.frame = CGRectMake(15, 10, self.width-30, self.width-30);
    _RGBView.frame = CGRectMake(10, CGRectGetMaxY(_colorView.frame)+10, self.width-20, 120);
}

- (void)closeView:(UIButton *)sender
{
    if (self.closeBlock) {self.closeBlock(YES);}
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)dealloc
{
#ifdef DEBUG
    NSLog(@"JMColorContainer销毁 %s", __FUNCTION__);
#endif
}

@end
